#include "SelectionBoxHandler.hpp"
#include "Colors.hpp"
#include "GLPrimitives.h"
#include "GUIManager.h"

// Ctr
SelectionBoxHandler::SelectionBoxHandler()
{
	m_Min.SetColor(blue);
	m_Max.SetColor(red);
	m_Max.SetStart(Vector2D(0, 0));
	m_Max.SetEnd(Vector2D(3, 3));
	m_Min.SetStart(Vector2D(1, 1));
	m_Min.SetEnd(Vector2D(2, 2));

	m_SelectingMin = false;
	m_SelectingMax = false;
}

// Render's the selection box
void SelectionBoxHandler::Render(ClippingPlane* pClippingPlane)
{
	m_Max.Render(pClippingPlane);
	//m_Min.Render(pClippingPlane);
}

// Options: "max x1y1", "max x2y2", "min x1y1", "min x2y2"
Vector2D SelectionBoxHandler::GetLocation(const string& variable)
{
	if(variable == "max x1y1")
		return m_Max.GetCorrectedStart();
	
	if(variable == "max x2y2")
		return m_Max.GetCorrectedEnd();

	if(variable == "min x1y1")
		return m_Min.GetCorrectedStart();

	if(variable == "min x2y2")
		return m_Min.GetCorrectedEnd();

	return Vector2D(0, 0);
}
void SelectionBoxHandler::HandleEvents(Uint8* keystates, const Vector2D& mouse, SDL_Event* event)
{

	// mouse selection
	if(event->type == SDL_MOUSEBUTTONDOWN && event->button.button == SDL_BUTTON_LEFT)
	{
		m_Max.SetStart(mouse);
		//m_Min.SetStart(mouse * 1.1);
		m_SelectingMax = true;
		
	}
	if(m_SelectingMax)
	{
		m_Max.SetEnd(mouse);
		//m_Min.SetEnd(mouse * .95);
		if(event->type == SDL_MOUSEBUTTONUP)
			m_SelectingMax = false;
	}

	if(event->type == SDL_MOUSEBUTTONDOWN && event->button.button == SDL_BUTTON_RIGHT)
	{
//		m_Min.SetStart(mouse);
//		m_SelectingMin = true;
	}
	else if(m_SelectingMin)
	{
//		m_Min.SetEnd(mouse);
//		if(event->type == SDL_MOUSEBUTTONUP)
//			m_SelectingMin = false;
	}
	/*else
	{
		m_Min.SetStart(m_Max.GetCorrectedStart() * 1.1);
		m_Min.SetEnd(m_Max.GetCorrectedEnd() * .9);
	}*/


	// Maximum
	if(keystates[SDLK_q])
		//if(m_Min.OutOfBounds(mouse))
			m_Max.SetStart(mouse);
	if(keystates[SDLK_w])
		//if(m_Min.OutOfBounds(mouse))
			m_Max.SetEnd(mouse);
	
	// Minumum
	if(keystates[SDLK_a])
		if(m_Max.InBounds(mouse))
			m_Min.SetStart(mouse);
	if(keystates[SDLK_s])
		if(m_Max.InBounds(mouse))
			m_Min.SetEnd(mouse);

}